﻿namespace Little.Soldier.SWIFT.Validation
{
  public class ValidationRule_Length : ValidationRuleBase
  {
    private int U0001;
    private int U0002;

    public ValidationRule_Length(int min, int max)
    {
      this.U0001 = min;
      this.U0002 = max;
    }

    public ValidationRule_Length(int min, int max, char delimiter, int count)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.Delimiter = delimiter;
      this.Count = count;
    }

    public ValidationRule_Length(int min, int max, int valueStartIndex)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.StartIndex = valueStartIndex;
    }

    public ValidationRule_Length(int min, int max, int valueStartIndex, int valueLength)
    {
      this.U0001 = min;
      this.U0002 = max;
      this.StartIndex = valueStartIndex;
      this.Length = valueLength;
    }

    public ValidationRule_Length(int length)
      : this(length, length)
    {
    }

    public override ValidationError[] Validate(string value)
    {
      if (value.Length >= this.U0001 && value.Length <= this.U0002)
        return new ValidationError[0];
      if (string.IsNullOrEmpty(this.ErrorDescription))
      {
        if (this.U0001 == this.U0002)
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongLength, string.Format("The length of the value '{1}' must be {0}.", (object) this.U0001, (object) value))
          };
        else
          return new ValidationError[1]
          {
            new ValidationError(ValidationErrorType.WrongLength, string.Format("The length of the value '{2}' must be between {0} and {1}.", (object) this.U0001, (object) this.U0002, (object) value))
          };
      }
      else
        return new ValidationError[1]
        {
          new ValidationError(ValidationErrorType.WrongLength, this.ErrorDescription)
        };
    }
  }
}
